package fr.lip6.meta.ple.generation.fh.java;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gmt.modisco.java.*;

import fr.lip6.meta.ple.generation.strategy.ReadXmiStrategy;

public class ReadXmiFHJava extends ReadXmiStrategy {

	@Override
	public String getMethodDeclarationAndBodyText(MethodDeclaration m) {
		StringBuilder res = new StringBuilder();

		if(m != null) {
			String returntype = m.getReturnType().getType().getName();

			res.append("\t"+getMethodDeclarationText(m));
			res.append(" {\n");

			org.eclipse.gmt.modisco.java.Block b = m.getBody();

			if (b != null) {
				EList<Statement> sts = b.getStatements();

				for(Statement st : sts) {
					res.append("\t\t"+getStatement(st));
					res.append("\n");
				}

				if (returntype.equals("void")) {
					res.append("\t}\n");
				} else {
					res.append("\t}\n");
				}
			}
		}

		return res.toString();
	}

	@Override
	public String getMethodDeclarationText(MethodDeclaration m) {
		StringBuilder res = new StringBuilder();

		if (m!=null) {
			String vis = getMethodModifierTextWithSpace(m);
			String returntype = m.getReturnType().getType().getName();
			EList<SingleVariableDeclaration> parmetres = m.getParameters();
			String method = vis+returntype +" "+m.getName()+"(";
			int i = 0;

			if(parmetres.size() == 0) {
				method += " )";
			} else {
				for(SingleVariableDeclaration var: parmetres) {
					i++;
					if(i != parmetres.size()) {
						method += var.getType().getType().getName() +" "+var.getName()+",";
					} else {
						method += var.getType().getType().getName() +" "+var.getName()+")";
					}

				}
			}
			res.append(method);
		}
		return res.toString();
	}

	@Override
	public String getConstructorDeclarationAndBodyText(ConstructorDeclaration consDecl) {
		StringBuilder res = new StringBuilder();
		res.append("\t"+getConstructorDeclarationText(consDecl));

		res.append(" {\n");

		org.eclipse.gmt.modisco.java.Block b = consDecl.getBody();

		if (b != null) {
			EList<Statement> sts = b.getStatements();

			for(Statement st : sts) {
				res.append("\t\t"+getStatement(st));
				res.append("\n");
			}
		}

		res.append("\t}\n");

		return res.toString();
	}

	@Override
	public String getConstructorDeclarationText(ConstructorDeclaration consDecl) {
		StringBuilder res = new StringBuilder();
		String vis = getConstructorModifierTextWithSpace(consDecl);
		EList<SingleVariableDeclaration> parameters = consDecl.getParameters();
		String method = vis+consDecl.getName()+"(";
		int i=0;

		if(parameters.size() == 0) {
			method += ")";
		} else {
			for(SingleVariableDeclaration var : parameters) {
				i++;
				if(i!=parameters.size()) {
					method += var.getType().getType().getName() +" "+var.getName()+",";
				} else {
					method += var.getType().getType().getName() +" "+var.getName()+")";
				}
			}
		}

		res.append(method);

		return res.toString();
	}
}
